package whf.framework.mail.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import whf.framework.exception.CreateException;
import whf.framework.exception.FindException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.exception.UpdateException;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.mail.dao.MailDAO;
import whf.framework.mail.entity.IMailBO;
import whf.framework.mail.entity.Mail;
import whf.framework.mail.exception.MailSendException;
import whf.framework.service.ServiceImp;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;

/**
 * @author wanghaifeng
 * @create Oct 18, 2006 10:25:18 PM
 * 
 */
public class MailServiceImp extends ServiceImp<Mail> implements MailService {
	private static Log log = LogFactory.getLog(MailServiceImp.class);
	/**
	 * @property MailDAO:mailDAO
	 */
	private MailDAO mailDAO;
	
	private SimpleMailMessage mailMessage;
	
	private MailSender mailSender;
	
	public void setMailDAO(MailDAO maildao){
		this.mailDAO = maildao;
	}
	
	public void setMailMessage(SimpleMailMessage msg){
		this.mailMessage = msg;
	}
	
	public void setMailSender(MailSender sender){
		this.mailSender = sender;
	}
	
	public static MailService getMailService() throws ServiceNotFoundException{
		return (MailService)BeanFactory.getService(MailServiceImp.class);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.mail.service.MailService#send(whf.framework.mail.entity.Mail)
	 */
	public void send(IMailBO mail) throws MailSendException, UpdateException {
		SimpleMailMessage msg = new SimpleMailMessage(this.mailMessage);
		if(!StringUtils.isEmpty(mail.getTo()))
			msg.setTo(StringUtils.split(mail.getTo(), ","));
		if(!StringUtils.isEmpty(mail.getCc()))
			msg.setCc(StringUtils.split(mail.getCc(), ","));
		if(!StringUtils.isEmpty(mail.getBcc()))
			msg.setBcc(StringUtils.split(mail.getBcc(), ","));
		
		if(!StringUtils.isEmpty(mail.getSubject()))
			msg.setSubject(mail.getSubject());
		msg.setText(mail.getBody());
		try{
			this.mailSender.send(msg);
			if(mail instanceof Mail){
				Mail m = (Mail)mail;
				m.setLastestSendTime(new Timestamp(System.currentTimeMillis()));
				m.setHasSent(true);
				m.setSendTimes(m.getSendTimes() + 1);
				if(m.getId() > 0){
					this.mailDAO.update(m);
				} else {
					this.mailDAO.create(m);
				}
			}
		}catch(UpdateException e){
			throw e;
		}catch(MailException e){
			if(mail instanceof Mail){
				Mail m = (Mail)mail;
				m.setLastestSendTime(new Timestamp(System.currentTimeMillis()));
				m.setHasSent(false);
				m.setSendTimes(m.getSendTimes() + 1);
				m.setLastestSendErrors(StringUtils.getExceptionDetail(e));
				if(m.getId() > 0)
					this.mailDAO.update(m);
				else {
					try{
						this.mailDAO.create(m);
					}catch(CreateException ce){
						log.error(this, ce);
					}
				}
			}
			throw new MailSendException(e);
		}catch (Exception e){
			log.error(this, e);
			throw new MailSendException(e);
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.mail.service.MailService#findNotSentMails()
	 */
	public List<Mail> findNotSentMails() throws FindException {
		return super.find("t.hasSent=?", Boolean.FALSE);
	}

}
